﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Threading;
using Styx;
using Styx.Helpers;
using Styx.CommonBot;
using Styx.Common;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System.Windows;

namespace Ringo
{
    public partial class Ringo
    {
        private static string SubversionRevision { get { return ("$Rev: 100 $"); } }
        #region Bools
        public static bool EnableLevel = false;
        public static bool EnableHonor = false;
        public static bool EnableItem = false;
        public static bool EnableTime = false;
        public static bool EnableHearth = false;
        public static bool OkayToTakeOverThread()
        {
            if (MeLP.Combat)
            {
                return false;
            }
            if (MeLP.IsDead)
            {
                return false;
            }
            if (MeLP.IsInInstance)
            {
                return false;
            }
            if (MeLP.IsFalling)
            {
                return false;
            }
            if (MeLP.IsCasting)
            {
                return false;
            }
            return true;
        }
        #endregion
        #region WoW Specific
        public static WoWUnit MeWU = Styx.StyxWoW.Me;
        public static LocalPlayer MeLP = Styx.StyxWoW.Me;
        #endregion
        #region Numbers
        public static int LogoutLevel;
        public static int LogoutHonor;
        public static int CurrentHonor
        {
            get
            {
                return Lua.GetReturnVal<int>("return GetCurrencyInfo(392)", 1);
            }
        }
        public static int LogoutItemQuantity;
        public static uint LogoutItemID;
        public static DateTime LogOutTime = DateTime.Now.AddYears(1);
        #endregion
        #region Voids
        public static void LogoutLevelCheck()
        {
            if (Styx.StyxWoW.Me.Level >= LogoutLevel)
            {
                NLog("You have reached level " + LogoutLevel + ", starting log out sequence.");
                Logout();
            }
        }
        public static void LogoutHonorCheck()
        {
            if (CurrentHonor >= LogoutHonor)
            {
                NLog("You have reached " + CurrentHonor + " honor, starting log out sequence.");
                Logout();
            }
        }
        public static void LogoutItemCheck()
        {
            uint TempItemQuantity = 0;
            foreach (WoWItem BagItems in ObjectManager.GetObjectsOfType<WoWItem>())
            {
                if (BagItems.Entry.Equals(LogoutItemID) && BagItems.BagSlot != -1)
                {
                    TempItemQuantity = TempItemQuantity + BagItems.StackCount;
                }
            }
            if (TempItemQuantity >= LogoutItemQuantity)
            {
                NLog("You have " + LogoutItemQuantity + " of Item ID " + LogoutItemID + ", starting log out sequence.");
                Logout();
            }
        }
        public static void LogoutTimeCheck()
        {
            if (DateTime.Now >= LogOutTime)
            {
                NLog("You have reached your logout time, starting log out sequence.");
                Logout();
            }
        }
        public static void Logout()
        {
            if (EnableHearth)
            {
                while (MeWU.IsFlying)
                {
                    WoWMovement.Move(WoWMovement.MovementDirection.Descend);
                    Thread.Sleep(200);
                }
                WoWMovement.MoveStop();
                NLog("Ringo Log Me Out is hearthing before logging out.");
                Mount.Dismount();
                Thread.Sleep(2000);
                Styx.WoWInternals.Lua.DoString("UseItemByName(\"Hearthstone\")");
                Thread.Sleep(2000);
                while (MeWU.IsCasting)
                {
                    Thread.Sleep(100);
                }
                Thread.Sleep(5000);
            }
            if (!Styx.StyxWoW.Me.IsDead && !Styx.StyxWoW.Me.Combat && StyxWoW.IsInGame)
            {
                WoWMovement.MoveStop();
                NLog("Ringo Log Me Out is logging out, goodbye.");
                Styx.WoWInternals.Lua.DoString("ForceQuit()");
            }
        }
        public static void DLog(string Text)
        {
            Logging.WriteDiagnostic("[RINGO PLUGINS] " + Text);
        }
        public static void NLog(string Text)
        {
            Logging.Write("[RINGO PLUGINS] " + Text);
        }
        #endregion
    }
}